package com.web.admin.web.base.auth;

import com.blue.core.dict.HttpMethod;
import com.blue.core.dict.annotation.DictParser;
import com.blue.http.annotation.Http;
import com.blue.http.message.HttpRequest;
import com.blue.http.message.View;
import com.blue.jdbc.Page;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.base.auth.facade.AppFacade;
import com.web.base.auth.message.AppRequest;
import com.web.base.auth.message.AppResponse;
import com.web.core.util.Const;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;

@Controller("authAppController")
@Http(url = "/base/auth")
public class AppController
{
	@Resource
	private AppFacade appFacade;
	
	private DictParser dictParser = DictParser.getInstance();
	
	public AppController()
	{
	}
	
	@Http(url = "/app_list", method = HttpMethod.GET)
	public View listGet(HttpRequest request)
	{
		AppRequest appRequest = new AppRequest();
		Page<AppResponse> page = appFacade.listPage(appRequest, Const.PAGE_START, Const.PAGE_SIZE);
		return View.createBuilder().setView("/base/auth/app_list")
				.put("model", appRequest)
				.put("map", dictParser.getStringMap())
				.put("page", page)
				.build();
	}
	
	@Http(url = "/app_list", method = HttpMethod.POST)
	public View listPost(HttpRequest request)
	{
		AppRequest appRequest = request.getPostObject(AppRequest.class);
		int start = request.getPostInt("start", Const.PAGE_START);
		Page<AppResponse> page = appFacade.listPage(appRequest, start, Const.PAGE_SIZE);
		return View.createBuilder().setView("/base/auth/app_list_grid")
				.put("page", page)
				.build();
	}
	
	@Http(url = "/app_get", method = HttpMethod.GET)
	public View get(HttpRequest request)
	{
		int id = request.getQueryInt("id", 0);
		AppResponse response = null;
		if (id > 0)
		{
			response = appFacade.get(id);
		}
		response = response == null ? new AppResponse() : response;
		return View.createBuilder().setView("/base/auth/app_get")
				.put("model", response)
				.put("map", dictParser.getStringMap())
				.build();
	}
	
	@Http(url = "/app_save", method = HttpMethod.POST)
	public void save(HttpRequest request)
	{
		AppRequest appRequest = request.getPostObject(AppRequest.class);
		AppResponse response = null;
		if (appRequest.getId() == null)
		{
			ValidationUtil.valid(appRequest, SaveModel.class);
			response = appFacade.add(appRequest);
		}
		else
		{
			ValidationUtil.valid(appRequest, UpdateModel.class);
			response = appFacade.update(appRequest);
		}
	}
	
	@Http(url = "/app_refresh", method = HttpMethod.POST)
	public void refresh(HttpRequest request)
	{
		appFacade.refresh();
	}
	
}
